﻿using System;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;

namespace Blitzcrank
{
    public partial class MainForm : Form
    {
        // x = 174, y=396
        private Thread _gameThread;
        private Bot _blitzBot = new Bot();
        private Bitmap _boardImage = new Bitmap(320, 320);
        private Graphics _boardGraphics;

        public MainForm()
        {
            InitializeComponent();
        }

        private void ThreadScan()
        {
            try
            {
                long start = DateTime.Now.Ticks;
                long diff = 0;
                _boardGraphics = Graphics.FromImage(_boardImage);
                while (diff < 700000000)
                {
                    _blitzBot.blitzBoard.GenerateBoard();
                    //textBox1.Text = PixelColorClass.GetPixelColorByImage(Cursor.Position.X, Cursor.Position.Y).GetSaturation().ToString();
                    //textBox2.Text = PixelColorClass.GetPixelColorByImage(Cursor.Position.X, Cursor.Position.Y).ToString();
                    for (int y = 0; y < 8; y++)
                    {
                        for (int x = 0; x < 8; x++)
                        {
                            switch ((ColorNames)_blitzBot.blitzBoard.board[y, x])
                            {
                                case ColorNames.Black:
                                    _boardGraphics.FillRectangle(Brushes.Black, new Rectangle(x * 40, y * 40, 40, 40));
                                    break;
                                case ColorNames.Blue:
                                    _boardGraphics.FillRectangle(Brushes.Blue, new Rectangle(x * 40, y * 40, 40, 40));
                                    break;
                                case ColorNames.Green:
                                    _boardGraphics.FillRectangle(Brushes.Green, new Rectangle(x * 40, y * 40, 40, 40));
                                    break;
                                case ColorNames.Orange:
                                    _boardGraphics.FillRectangle(Brushes.Orange, new Rectangle(x * 40, y * 40, 40, 40));
                                    break;
                                case ColorNames.Purple:
                                    _boardGraphics.FillRectangle(Brushes.Purple, new Rectangle(x * 40, y * 40, 40, 40));
                                    break;
                                case ColorNames.Red:
                                    _boardGraphics.FillRectangle(Brushes.Red, new Rectangle(x * 40, y * 40, 40, 40));
                                    break;
                                case ColorNames.White:
                                    _boardGraphics.FillRectangle(Brushes.White, new Rectangle(x * 40, y * 40, 40, 40));
                                    break;
                                case ColorNames.Yellow:
                                    _boardGraphics.FillRectangle(Brushes.Yellow, new Rectangle(x * 40, y * 40, 40, 40));
                                    break;
                            }
                        }
                    }
                    pictureBox1.Image = _boardImage;
                    _blitzBot.GetPoints();
                    diff = DateTime.Now.Ticks - start;
                    textBox1.Text = diff.ToString();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void btnInitialize_Click(object sender, EventArgs e)
        {
            if (!_blitzBot.blitzBoard.gotPosition)
            {
                _blitzBot.blitzBoard.SetUpperLeftPosition();
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            _gameThread = new Thread(new ThreadStart(ThreadScan));
            _gameThread.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            _blitzBot.blitzBoard.gotPosition = false;
            _gameThread.Abort();
        }
    }
}
